package com.likeyoudo.views.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.tabs.TabLayoutMediator;
import com.likeyoudo.common.BaseActivity;
import com.likeyoudo.common.msg.MsgEvent;
import com.likeyoudo.views.R;
import com.likeyoudo.views.adapter.ImagePreviewFragmentAdapter;
import com.likeyoudo.views.constants.xg.ApprovalConstants;
import com.likeyoudo.views.databinding.ActivityImagePreviewBinding;
import com.likeyoudo.views.fragment.ImagePreviewFragment;
import com.likeyoudo.views.utils.HttpRequest;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LikeYouDo
 * @date 2024/1/8 16:13
 */
public class ImagePreviewActivity extends BaseActivity {

    List<ImagePreviewFragment> list = new ArrayList<>();
    ActivityImagePreviewBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityImagePreviewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setImmersiveWindows();
        showBackButton();
        setTitle("图片预览");

        // 获取Intent实例
        Intent intent = getIntent();

        // 获取字符串数据
        String pictures = intent.getStringExtra(ApprovalConstants.PICS_KEY);
        if (pictures != null) {
            // 使用传递过来的字符串数据
            String[] split = pictures.split(",");
            // 创建Fragment
            for (String s : split) {
                list.add(new ImagePreviewFragment(HttpRequest.HOST + s));
            }
            ImagePreviewFragmentAdapter imagePreviewFragmentAdapter = new ImagePreviewFragmentAdapter(getSupportFragmentManager(), getLifecycle(), list);
            binding.vp2Image.setAdapter(imagePreviewFragmentAdapter);
            // TabLayout与ViewPager2联动
            new TabLayoutMediator(binding.tab, binding.vp2Image,
                    (tab, position) -> {
                        tab.setText("图片 " + (position + 1)); // 设置标签文本
                    }
            ).attach();
        } else {
            ToastUtils.showShort("图片预览失败");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image_preview;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MsgEvent event) {

    }
}
